package com.framework.wz.wutong.utils;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.v4.app.ActivityCompat;
import android.util.DisplayMetrics;
import android.util.TypedValue;

import com.framework.wz.wutong.base.App;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class OtherUtils {

    /**
     * @bref 获取屏幕宽度
     */
    public static int getDisplayWidth(Context context) {
        DisplayMetrics me    = context.getResources().getDisplayMetrics();
        int            width = me.widthPixels;
        return width;
    }

    /**
     * @bref 获取屏幕高度
     */
    public static int getDisplayHeight(Context context) {
        DisplayMetrics me     = context.getResources().getDisplayMetrics();
        int            height = me.heightPixels;
        return height;
    }

    /**
     * @bref 获取屏幕密度
     */
    public static float getDisplayDensity(Context context) {
        DisplayMetrics me     = context.getResources().getDisplayMetrics();
        float          height = me.density;
        return height;
    }

    /**
     * @bref dip转化px
     */
    public static int dip2px(float dip, Context context) {
        DisplayMetrics me     = context.getResources().getDisplayMetrics();
        int            margin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dip, me);
        return margin;
    }

    /**
     * 获取应用的版本号
     *
     * @return
     * @date 2015-1-13
     */
    public static int getVersion(Context context) {
        try {
            PackageManager manager = context.getPackageManager();
            PackageInfo    info    = manager.getPackageInfo(context.getPackageName(), 0);
            int            version = info.versionCode;
            return version;
        } catch (Exception e) {
            e.printStackTrace();
            return -1;
        }
    }

    /**
     * @param timestamp 时间戳
     * @param patten    格式化格式，例如"yyyy-MM-dd HH:mm:ss"
     * @return 格式化时间字符串
     */
    @SuppressLint("SimpleDateFormat")
    public static String getDataFormatString(long timestamp, String patten) {
        SimpleDateFormat format = new SimpleDateFormat(patten);
        return format.format(timestamp);
    }

    /**
     * @param date 格式为yyyy-MM-dd HH:mm:ss的时间字符串
     * @return long型时间戳
     */
    @SuppressLint("SimpleDateFormat")
    public static long getMilliseconds(String date) {
        if (date == null || date.trim().equals("")) return -1;
        Date             mDate  = null;
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        try {
            mDate = format.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return mDate.getTime();
    }

    /**
     * 保留两位小数(四舍五入)
     *
     * @param a
     * @return
     */
    public static String get2Double(double a) {
        String     a1  = String.valueOf(a);
        BigDecimal bg3 = new BigDecimal(a1);
        BigDecimal f3  = bg3.setScale(2, BigDecimal.ROUND_HALF_UP);
        String     s   = String.valueOf(f3);
        return s;
    }

    public static String StringgetSerialNumber() {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

            if (ActivityCompat.checkSelfPermission(App.getInstance().getApplicationContext(), Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {

                return "";
            }

            try {

                return Build.getSerial();
            }catch (Exception e){

                e.printStackTrace();
            }

        }

        return Build.SERIAL;

    }
}
